﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

namespace MyDAL
{
    /// <summary>
    /// AccessDataBase 的摘要说明
    /// </summary>
    public class OleDbDataBase : IDataBase
    {
        #region 私有成员

        private string _connString = string.Empty;
        private OleDbConnection conn;
        private MyDAL.Enumeration.ConnStringType _connType = MyDAL.Enumeration.ConnStringType.ConfigKey;
        #endregion
        /// <summary>
        /// DataRead读取器事件
        /// </summary>
        public event MyDAL.DataBaseEvent.DataReadEvent DataReading;
        #region 属性
        /// <summary>
        /// 链接字符或ConfigKey
        /// </summary>
        public string ConnString
        {
            get { return _connString; }
            set { _connString = value; }
        }
        /// <summary>
        /// 链接字符类型,默认为ConfigKey
        /// </summary>
        public MyDAL.Enumeration.ConnStringType ConnType
        {
            get { return _connType; }
            set { _connType = value; }
        }
        #endregion
        /// <summary>
        /// 初始化实例
        /// </summary>
        /// <param name="dbUrl">虚拟路径,为非路径则取Config</param>
        /// <param name="pwd">密码,无则为空</param>
        public OleDbDataBase(string dbUrl, string pwd)
        {
          
            //
            // TODO: 在此处添加构造函数逻辑
            //
            this._connString = dbUrl;
            string strConnection = string.Empty;
            strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
                "Data Source=" + this._connString + ";Jet OLEDB:Database password=" + pwd;
  
            conn = new OleDbConnection(strConnection);
        }     
        /// <summary>
        /// 初始化实例
        /// </summary>
        /// <param name="constr">链接字符窜</param>
        /// <param name="type">链接类型</param>
        public OleDbDataBase(string constr, MyDAL.Enumeration.ConnStringType type)
        {

            //
            // TODO: 在此处添加构造函数逻辑
            //
            this._connString = constr;
            string strConnection = string.Empty;
            if (type == MyDAL.Enumeration.ConnStringType.ConfigKey)
            {
                strConnection = System.Configuration.ConfigurationManager.AppSettings[this._connString].ToString();
            }
            else
            {
                strConnection = this._connString;
            }
            conn = new OleDbConnection(strConnection);
        }
        /// <summary>
        /// 执行操作
        /// </summary>
        /// <param name="OleDb">OleDb语句</param>
        /// <returns>受影响的行数</returns>
        public int ExecuteNonQuery(string OleDb)
        {
            conn.Open();
            int i;
            try
            {
                OleDbCommand comm = new OleDbCommand(OleDb, conn);
                i = Convert.ToInt32(comm.ExecuteNonQuery());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return i;
        }
        /// <summary>
        /// 读取单行单列值
        /// </summary>
        /// <param name="OleDb">OleDb语句</param>
        /// <returns>对像值</returns>
        public object ExecuteScalar(string OleDb)
        {
            conn.Open();
            object i;
            try
            {
                OleDbCommand comm = new OleDbCommand(OleDb, conn);
                i = comm.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return i;
        }
        /// <summary>
        /// 获取OleDbDataReader读取器
        /// </summary>
        /// <param name="OleDb">该读取会一值保持链接</param>
        public void  ExecuteReader(string OleDb)
        {
            conn.Open();
            try
            {
                OleDbCommand comm = new OleDbCommand(OleDb, conn);
                OleDbDataReader dr = comm.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    DataReading(dr);
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }
        /// <summary>
        /// 返回一个数据集操作
        /// </summary>
        /// <param name="OleDb">OleDb语句</param>
        /// <returns>数据集</returns>
        public DataSet GetDataSet(string OleDb)
        {
            DataSet ds = new DataSet();
            conn.Open();
            try
            {
                OleDbDataAdapter da = new OleDbDataAdapter(OleDb, conn);
        
                da.Fill(ds);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return ds; 
        }
        /// <summary>
        //  返回一个数据表
        /// </summary>
        /// <param name="OleDb">OleDb语句</param>
        /// <returns>数据表</returns>
        public DataTable GetDataTable(string OleDb)
        {
            DataTable dt = new DataTable();
            conn.Open();
            try
            {
                OleDbDataAdapter da = new OleDbDataAdapter(OleDb, conn);
                da.Fill(dt);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return dt;
        }
    }
}